package com.relytosoft.mizar.analyze.job.task;

import cn.hutool.core.util.ObjectUtil;
import com.relytosoft.dubhe.common.api.entity.system.SysMsgRepeat;
import com.relytosoft.dubhe.common.api.service.RemoteMsgRepeatService;
import com.relytosoft.dubhe.common.kafka.producer.KafkaProducer;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Jierry
 * @version 2022/5/10.
 */
@Service
public class MqRepeatTask {

    @Resource
    private RemoteMsgRepeatService remoteMsgRepeatService;

    @Resource
    private KafkaProducer kafkaProducer;

    @XxlJob("MqRepeatTask")
    public void mqRepeatJobHandler() throws Exception {
        try {
            XxlJobHelper.log("MqRepeatTask-start");
            SysMsgRepeat msgRepeat = remoteMsgRepeatService.getNoHandleMsg();
            if (ObjectUtil.isNotEmpty(msgRepeat)) {
                kafkaProducer.send(msgRepeat.getTopicName(), msgRepeat.getContent(), false);
                remoteMsgRepeatService.updateSysMsgRepeatStatus(msgRepeat.getId(), "JobSystem");
                XxlJobHelper.log("MqRepeatTask-resend:" + msgRepeat.getTopicName());
            }
            XxlJobHelper.log("MqRepeatTask-end");
        } catch (Exception ex) {
            XxlJobHelper.log("MqRepeatTask-error:" + ex.getMessage());
        }
    }
}
